package com.fafa.controller;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/login")
    //@ResponseBody
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Model model, HttpServletRequest request){
        //如果账号不为空，密码等于“123456”即可登录成功
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
//            return "dashboard";
            HttpSession session = request.getSession();
            session.setAttribute("user",username);
            return "redirect:/main.html";
        }else{
            model.addAttribute("msg","账号或者密码错误!");
            return "login";
        }

    }
}
